from typing import Any, Optional
from pydantic import BaseModel, Field

from .task_data import TaskType, TaskPriority, TaskStatus


class TaskRequest(BaseModel):
    """创建任务请求模型"""

    name: str = Field(..., description="任务名称")
    type: TaskType = Field(..., description="任务类型")
    func_name: str = Field(..., description="执行函数名")
    args: list[Any] = Field(default=[], description="函数参数")
    kwargs: dict[str, Any] = Field(default_factory=dict, description="函数关键字参数")
    priority: TaskPriority = Field(
        default=TaskPriority.MEDIUM, description="任务优先级"
    )
    schedule_time: Optional[str] = Field(
        default=None, description="定时任务执行时间(ISO格式)"
    )
    interval: Optional[int] = Field(default=None, description="周期性任务间隔(秒)")
    timeout: Optional[int] = Field(default=None, description="超时时间(秒)")
    max_retries: int = Field(default=3, description="最大重试次数")


class TaskResponse(BaseModel):
    """任务响应模型"""

    id: str
    name: str
    type: TaskType
    status: TaskStatus
    priority: TaskPriority
    created_at: str
    started_at: Optional[str] = None
    completed_at: Optional[str] = None
    result: Optional[Any] = None
    error: Optional[str] = None
    retry_count: int = 0
    progress: Optional[float] = None  # 执行进度(0-100)
